#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,string
from ClusterUtility import GetEuclidDistance, ShowClusterResult
            
"""
算法名称：最小距离法聚类(Minimum distance clustering method)
@thresh 阈值
@DataSet 向量数据集
@return 聚类后的结果
"""
def SimpleCluster(thresh, DataSet):
    assert len(DataSet) >= 1
    v = DataSet[0]
    cluster = []
    ClusterSet = []
    cluster.append(v) #加入第一个向量，并作为中心向量
    ClusterSet.append(cluster) #将第一类加入聚类集中
    for v in DataSet[1:]:
        mindist = GetEuclidDistance(v, ClusterSet[0][0])
        cluster = ClusterSet[0]
        flag = True
        for c in ClusterSet[1:]:
            tmp = GetEuclidDistance(v, c[0])
            if tmp < mindist:
                mindist = tmp
                cluster = c
        if mindist > thresh: #判断最小的距离是否小于阈值
            flag = False
        if flag:
            cluster.append(v)
        else:
            cluster = []
            cluster.append(v)
            ClusterSet.append(cluster)
    ShowClusterResult(ClusterSet)

def test(argv):
    #在这里构造DataSet
    DataSet = []
    DataSet.append((1,2))
    DataSet.append((2,3))
    DataSet.append((5,6))
    DataSet.append((7,8))
    DataSet.append((3,4))
    if len(argv) != 2:
        print("Usage: python SimpleCluster.py <threshold>")
        return 
    else:
        thresh = string.atof(argv[1])
        #print(thresh)
        SimpleCluster(thresh, DataSet)
    return
if __name__ == "__main__": test(sys.argv)
    
